<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

</head>
<!-- 第一个helloworld程序 -->

<body>
  <div id="app"></div>
  <template id="root">
    <div>
      <button @click="login">原始登录</button>
      <button v-debounce="login">防抖登录</button>
    </div>
  </template>
</body>
<script>
  const app = Vue.createApp({
    template: "#root",
    methods: {
      login() {
        console.log("成功登录");
      }
    },
    directives: {
      debounce: {
        mounted(el, binding) {
          let timer = null
          el.addEventListener('click', () => {
            //防抖
            if (timer) {
              clearTimeout(timer)
            }
            timer = setTimeout(() => {
              binding.value()
            }, 1000)
          })
        }
      }
    }

  })
  app.mount("#app")
</script>

</html>